package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.Exception.CustomException;
import com.itheima.common.R;
import com.itheima.dao.DishDao;
import com.itheima.dao.DishFlavorDao;
import com.itheima.dao.EmployeeDao;
import com.itheima.pojo.Dish;
import com.itheima.pojo.DishFlavor;
import com.itheima.pojo.Employee;
import com.itheima.service.DishService;
import com.itheima.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class DishServiceImpl extends ServiceImpl<DishDao, Dish>  implements DishService {

    @Autowired
    DishDao dishDao;

    @Autowired
    DishFlavorDao dishFlavorDao;

    /**
     * 删除菜品
     */
    @Override
    @Transactional(rollbackFor = CustomException.class)
    public String deleteById(List<Long> ids) {
        //逻辑删除菜品
        int count = dishDao.deleteBatchIds(ids);
        if (count <= 0){
            try {
                throw new CustomException("删除失败");
            } catch (CustomException e) {
                throw new RuntimeException(e);
            }
        }
        //逻辑删除菜品的口味
        for (Long DishId : ids) {
            LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(DishFlavor::getDishId,DishId);
            dishFlavorDao.delete(wrapper);
        }
        return "删除成功";
    }
}
